package com.shop.util;

import org.apache.commons.lang3.StringUtils;

public class IdSignUtil {

    /** 加密key */
    // ZdEIw9wtsqrKAmDR
    private static final String key = "ZdEIw9wtsqrKAmDR";

    /**
     * 加密ID
     * @param id
     * @return
     */
    public static String doSign(Long id) {
        if(null == id || StringUtils.isBlank(key)) {
            return null;
        }

        try {
            return CodecUtil.buildDESEncrypt(id.toString(), key);
        } catch (Exception e) {
            // 加密失败，不做日志打印
        }

        return null;
    }

    /**
     * 解密ID
     * @param sign
     * @return
     */
    public static Long checkSign(String sign) {
        if(StringUtils.isBlank(sign)) {
            return null;
        }

        try {
            String idStr = CodecUtil.buildDESDecrypt(sign, key);
            return Long.valueOf(idStr);
        } catch (Exception e) {
            // 解密失败，不做日志打印
        }

        return null;
    }

    public static void main(String[] args) {
        System.out.println(IdSignUtil.doSign(2L));
        //System.out.println(IdSignUtil.checkSign("r0fFy1HvhEc"));


    }

}
